package me.goldze.mvvmhabit.utils;

import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import me.goldze.mvvmhabit.R;

/**
 * 吐司工具类
 */
public final class ToastUtils {

    private static boolean isShow = true;//默认显示
    private static Toast mToast = null;//全局唯一的Toast


    /**
     * 显示短时吐司
     *
     * @param format 格式
     * @param args   参数
     */
    public static void showShort(String format, Object... args) {
        show(String.format(format, args));
    }

    /**
     * 短时间显示Toast
     *
     * @param message
     */
    public static void showShort(CharSequence message) {
        show(message);
    }

    /**
     * 短时间显示Toast
     *
     * @param message
     */
    public static void show(CharSequence message) {
        if (mToast == null) {
            mToast = Toast.makeText(Utils.getContext(), message, Toast.LENGTH_SHORT);
            mToast.setGravity(Gravity.CENTER_HORIZONTAL, 0, ScreenUtil.dp2px(200));
            View view = View.inflate(Utils.getContext(), R.layout.toast_layout, null);
            TextView toast_tv = view.findViewById(R.id.toast_tv);
            toast_tv.setText(message);
            mToast.setView(view);
        } else {
            TextView toast_tv = mToast.getView().findViewById(R.id.toast_tv);
            toast_tv.setText(message);
        }
        mToast.show();
    }

    /**
     * 取消吐司显示
     */
    public static void cancel() {
        if (mToast != null) {
            mToast.cancel();
        }
    }
}